<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="文档," />










<meta name="description" content="gradle官方文档">
<meta name="keywords" content="文档">
<meta property="og:type" content="article">
<meta property="og:title" content="gradle 指南之构建Java库">
<meta property="og:url" content="https://scorpiozhoulei.gitee.io/2018/05/10/gradle-指南之构建Java库/index.html">
<meta property="og:site_name" content="zlTuring的博客">
<meta property="og:description" content="gradle官方文档">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-05-20T15:25:42.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="gradle 指南之构建Java库">
<meta name="twitter:description" content="gradle官方文档">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://scorpiozhoulei.gitee.io/2018/05/10/gradle-指南之构建Java库/"/>





  <title>gradle 指南之构建Java库 | zlTuring的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">zlTuring的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">zlblog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://scorpiozhoulei.gitee.io/2018/05/10/gradle-指南之构建Java库/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zlTuring">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/imgs/Menhera-help.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlTuring的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">gradle 指南之构建Java库</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-10T23:01:46+08:00">
                2018-05-10
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/gradle/" itemprop="url" rel="index">
                    <span itemprop="name">gradle</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          
              <div class="post-description">
                  gradle官方文档
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="构建Java库"><a href="#构建Java库" class="headerlink" title="构建Java库"></a>构建Java库</h1><p>本指南引导您完成使用Gradle的Build Init插件生成适合其他JVM库和应用程序使用的JVM库的过程。</p>
<h2 id="你会建立什么"><a href="#你会建立什么" class="headerlink" title="你会建立什么"></a>你会建立什么</h2><p>您将使用标准布局生成Java库。</p>
<h2 id="你需要什么"><a href="#你需要什么" class="headerlink" title="你需要什么"></a>你需要什么</h2><ul>
<li>大约11分钟</li>
<li>文本编辑器或IDE</li>
<li>一个Java开发工具包（JDK），版本1.7或更高</li>
<li>一个<a href="https://gradle.org/install?_ga=2.154729144.1255618932.1525922524-29619511.1524466760" target="_blank" rel="noopener">Gradle</a>，版本4.7或更高</li>
</ul>
<h2 id="创建一个库项目"><a href="#创建一个库项目" class="headerlink" title="创建一个库项目"></a>创建一个库项目</h2><p>Gradle带有一个名为Build Init插件的内置插件。它在Gradle用户手册中有记录。该插件有一个叫做的任务init，用于生成项目。该init任务调用（也是内置的）wrapper任务来创建一个Gradle包装脚本，gradlew。</p>
<p>第一步是为新项目创建一个文件夹并将目录切换到其中。</p>
<pre><code>$ mkdir building-java-libraries
$ cd building-java-libraries
</code></pre><h2 id="运行init任务"><a href="#运行init任务" class="headerlink" title="运行init任务"></a>运行init任务</h2><p>从新项目目录中，init使用java-library参数运行任务。</p>
<pre><code>$ gradle init --type java-library
&gt;任务：包装
&gt;任务：初始化

4s制造成功
2个可执行的任务：2个执行
</code></pre><p>该init任务运行的wrapper任务首先，其产生gradlew和gradlew.bat包装脚本。然后它创建具有以下结构的新项目：</p>
<pre><code>。
├──build.gradle
├──gradle
│└──wrapper       1⃣️
│├──gradle-wrapper.jar
│└──gradle-wrapper.properties
├──gradlew
├──gradlew.bat
├──settings.gradle
└──src
    ├──main
    │└──java     2⃣️
    │└──Library.java
    └──test
        └──java    3⃣️
            └──LibraryTest.java
</code></pre><ol>
<li>为包装文件生成文件夹</li>
<li>默认的Java源文件夹</li>
<li>默认的Java测试文件夹</li>
</ol>
<p>您现在拥有一个简单的Java库项目所需的组件。</p>
<h2 id="查看生成的项目文件"><a href="#查看生成的项目文件" class="headerlink" title="查看生成的项目文件"></a>查看生成的项目文件</h2><p>该<strong>settings.gradle</strong>文件受到严重评论，但只有一条活动行：<br>生成的设置</p>
<pre><code>rootProject.name=&apos;building-java-libraries&apos;  1⃣️
</code></pre><ol>
<li>这将分配根项目的名称。</li>
</ol>
<p>生成的build.gradle文件也有很多评论。这里重现活动部分（注意依赖关系的版本号可能会在更新版本的Gradle中更新）：</p>
<pre><code>plugins {
    id &apos;java-library&apos;
}

dependencies {
    api &apos;org.apache.commons:commons-math3:3.6.1&apos;   2⃣️

    implementation &apos;com.google.guava:guava:23.0&apos;   3⃣️

    testImplementation &apos;junit:junit:4.12&apos;    4⃣️
}

repositories {
    jcenter()   1⃣️
}
version = &apos;0.1.0&apos;

jar {
    manifest {
        attributes(&apos;Implementation-Title&apos;: project.name,
                   &apos;Implementation-Version&apos;: project.version)
    }
}
</code></pre><ol>
<li>公共Bintray Artifactory存储库</li>
<li>这是一个导出到消费者的依赖项的例子，也就是说在他们的编译类路径中找到。</li>
<li>这是一个内部使用的依赖关系的实例，并且不会在自己的编译类路径中向消费者公开。</li>
<li>JUnit测试库</li>
</ol>
<p>该build.gradle添加的Java库插件。这是该java-base插件的扩展，并添加了编译Java源代码的其他任务。</p>
<p>该文件src/main/java/Library.java显示在这里：</p>
<pre><code>public class Library {
    public boolean someLibraryMethod() {
        return true;
    }
}
</code></pre><p>下面显示了生成的JUnit规范src/test/java/LibraryTest.java：</p>
<pre><code>import org.junit.Test;

public class LibraryTest {
    @Test public void testSomeLibraryMethod() {
        Library classUnderTest = new Library();
        assertTrue(&quot;someLibraryMethod should return &apos;true&apos;&quot;, classUnderTest.someLibraryMethod());
    }
}
</code></pre><p>生成的测试类只有一个JUnit 4测试。测试实例化Library类，调用someLibraryMethod方法并检查返回的值是否为true。</p>
<h2 id="组装库JAR"><a href="#组装库JAR" class="headerlink" title="组装库JAR"></a>组装库JAR</h2><p>要构建该项目，请运行该build任务。你可以使用普通的gradle命令，但是当一个项目包含一个包装脚本时，可以使用它来代替它。</p>
<pre><code>$ ./gradlew构建
&gt;任务：compileJava
&gt;任务：processResources NO-SOURCE
&gt;任务：类
&gt;任务：jar
&gt;任务：组装
&gt;任务：compileTestJava
&gt;任务：processTestResources NO-SOURCE
&gt;任务：testClasses

&gt;任务：测试
拿起_JAVA_OPTIONS：-Xmx2048m -Xms512m

&gt;任务：检查
&gt;任务：构建

8s成功制造
4个可执行任务：4个执行
</code></pre><blockquote><p>第一次运行包装脚本时gradlew，可能会有一个延迟，而该版本的gradle下载和本地存储在您的<br>~/.gradle/wrapper/dists 文件夹中。</p>
</blockquote>
<p>第一次运行构建时，Gradle将检查您的~/.gradle目录下是否已在缓存中拥有JUnit库和其他列出的依赖项。如果没有，库将被下载并存储在那里。下一次运行构建时，将使用缓存版本。该build任务编译类，运行测试并生成测试报告。<br>您可以通过打开位于的HTML输出文件来查看测试报告 build/reports/tests/test/index.html。</p>
<h2 id="自定义库JAR"><a href="#自定义库JAR" class="headerlink" title="自定义库JAR"></a>自定义库JAR</h2><h2 id="添加API文档"><a href="#添加API文档" class="headerlink" title="添加API文档"></a>添加API文档</h2><h1 id="概要"><a href="#概要" class="headerlink" title="概要"></a>概要</h1>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/文档/" rel="tag"># 文档</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/10/gradle指南之创建新的Gradle构建/" rel="next" title="gradle 指南之创建新的Gradle构建">
                <i class="fa fa-chevron-left"></i> gradle 指南之创建新的Gradle构建
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/05/20/gradle-指南之构建java-web应用程序/" rel="prev" title="gradle 指南之构建java web应用程序">
                gradle 指南之构建java web应用程序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/imgs/Menhera-help.jpg"
                alt="zlTuring" />
            
              <p class="site-author-name" itemprop="name">zlTuring</p>
              <p class="site-description motion-element" itemprop="description">zlTuring的博客</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#构建Java库"><span class="nav-number">1.</span> <span class="nav-text">构建Java库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#你会建立什么"><span class="nav-number">1.1.</span> <span class="nav-text">你会建立什么</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#你需要什么"><span class="nav-number">1.2.</span> <span class="nav-text">你需要什么</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建一个库项目"><span class="nav-number">1.3.</span> <span class="nav-text">创建一个库项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运行init任务"><span class="nav-number">1.4.</span> <span class="nav-text">运行init任务</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#查看生成的项目文件"><span class="nav-number">1.5.</span> <span class="nav-text">查看生成的项目文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#组装库JAR"><span class="nav-number">1.6.</span> <span class="nav-text">组装库JAR</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自定义库JAR"><span class="nav-number">1.7.</span> <span class="nav-text">自定义库JAR</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加API文档"><span class="nav-number">1.8.</span> <span class="nav-text">添加API文档</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#概要"><span class="nav-number">2.</span> <span class="nav-text">概要</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zlTuring</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
